package com.summer.learn.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 在 DispatcherServlet的 doDispatch方法中使用
 */
public class FirstInterceptor implements HandlerInterceptor {

    /**
     * 该方法在目标方法之前被调用。
     * 若返回值为true,则继续调用后续的拦截器和目标方法。
     * 若返回值为false,则不会再调用后续的拦截器和目标方法。
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("FirstInterceptor preHandle");
        return true;
    }

    /**
     * 调用目标方法之后,但渲染视图之前.
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("FirstInterceptor postHandle");
    }

    /**
     * 渲染视图之后调用
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("FirstInterceptor afterCompletion");
    }
}
